package com.example.springwebmvcdemo.controller;

import com.example.springwebmvcdemo.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/param")
public class ParamController {
    @RequestMapping("/p1")
    public String p1(String name) {
        return "接收到参数,name: " + name;
    }

    //更多使用包装类
    @RequestMapping("/p2")
    public String p2(Integer age) {
        return "接收到参数,age: " + age;
    }

    @RequestMapping("/p3")
    public String p3(int age) {
        return "接收到参数,age: " + age;
    }

    //多个参数
    @RequestMapping("/p4")
    public String p4(String name,int age) {
        return "接收到参数,name: " + name + ",age: " + age;
    }

    @RequestMapping("/p5")
    public String p5(String name,int age,int gender) {
        return "接收到参数,name: " + name + ",age: " + age + ",gender: " + gender;
    }

    //传递对象
    @RequestMapping("/p6")
    public String p6(User user) {
        return "user: " + user;
    }

    //参数重命名,当前端命名规则与后端冲突时...
    //@RequestParam参数绑定
    //此时Postman中的 参数名 必须与value一致,即传参名为 userName
    //required = false 修改必填参数为 flase
    @RequestMapping("/p7")
    public String p7(@RequestParam(value = "userName",required = false) String name) {
        return "name: " + name;
    }

    //传字符数组
    @RequestMapping("/p8")
    public String p8(String[] arr) {
        return "arr: " + List.of(arr);
    }

    //传递List
    //@RequestParam参数绑定
    @RequestMapping("/p9")
    public String p9(@RequestParam List<String> list) {
        return "list: " + list;
    }

    //传json格式的类,转化为java格式的类
    //在postman中的Body->form-data中写参数(java格式)raw中写json代码,再点send!!!!!!
    @RequestMapping("/p10")
    public String p10(@RequestBody User user) {
        return "user; " + user;
    }

    //.............................................
    //获取URL中的参数
    @RequestMapping("/article/{articleId}")
    public String p11(@PathVariable("articleId") String articleId) {
        return "articleId: " + articleId;
    }

    @RequestMapping("/article/{articleId}/{name}")
    public String p12(@PathVariable("articleId") String id, @PathVariable String name) {
        return "articleId: " + id + ", name: " + name;
    }

    //获取文件名
    @RequestMapping("/p13")
    public String p13(@RequestPart("fileName") MultipartFile file) {
        System.out.println(file.getOriginalFilename());
        return "文件名称: " + file.getOriginalFilename();
    }
}
